unit DBOptions;

interface

uses
  Uni, MySQLUniProvider, SysUtils;

type
  TDBAdminOptions = class
  private
    FModified: boolean;
    FAutoBackup: boolean;
    FBackupPath: string;
    FMySQLBin: string;
    procedure SetModified(const Value: boolean);
    procedure SetAutoBackup(const Value: boolean);
    procedure SetBackupPath(const Value: string);
    procedure SetMySQLBin(const Value: string);
  public
    constructor Create;
    property Modified: boolean read FModified write SetModified;
    property BackupPath: string read FBackupPath write SetBackupPath;
    property AutoBackup: boolean read FAutoBackup write SetAutoBackup;
    property MySQLBin: string read FMySQLBin write SetMySQLBin;
  end;

  TDBPaymentOptions = class
  private
    FModified: boolean;
    FLastPaymentType: integer;
    procedure SetLastPaymentType(const Value: integer);
    procedure SetModified(const Value: boolean);
  public
    constructor Create;
    property Modified: boolean read FModified write SetModified;
    property LastPaymentType: integer read FLastPaymentType write SetLastPaymentType;
  end;

  TDBOrderOptions = class
  private
    FReadyWarningDays: integer;
    FModified: boolean;
    FReadyWarningColor: integer;
    procedure SetReadyWarningDays(const Value: integer);
    procedure SetModified(const Value: boolean);
    procedure SetReadyWarningColor(const Value: integer);
  public
    constructor Create;
    property Modified: boolean read FModified write SetModified;
    property ReadyWarningDays: integer read FReadyWarningDays write SetReadyWarningDays;
    property ReadyWarningColor: integer read FReadyWarningColor write SetReadyWarningColor;
  end;

  TDBClientOptions = class
  private
    FModified: boolean;
    FClientCodeTemplate: string;
    procedure SetModified(const Value: boolean);
    procedure SetClientCodeTemplate(const Value: string);
  public
    constructor Create;
    property Modified: boolean read FModified write SetModified;
    property ClientCodeTemplate: string read FClientCodeTemplate write SetClientCodeTemplate;
  end;

  TDBOptions = class
  private
    FUserId: integer;
    FOrder: TDBOrderOptions;
    FPayment: TDBPaymentOptions;
    FAdmin: TDBAdminOptions;
    FClient: TDBClientOptions;

    procedure SetOrder(const Value: TDBOrderOptions);
    function GetModified: boolean;
    procedure SetModified(const Value: boolean);
    procedure SetPayment(const Value: TDBPaymentOptions);
    procedure SetAdmin(const Value: TDBAdminOptions);
    procedure SetClient(const Value: TDBClientOptions);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Load(AQuery: TUniQuery; AUserId: integer);
    procedure Save(AQuery: TUniQuery; AUserId: integer = -1);
    property Order: TDBOrderOptions read FOrder write SetOrder;
    property Payment: TDBPaymentOptions read FPayment write SetPayment;
    property Admin: TDBAdminOptions read FAdmin write SetAdmin;
    property Client: TDBClientOptions read FClient write SetClient;
    property Modified: boolean read GetModified write SetModified;
  end;

implementation

const
  WARN_READY_DAYS = 1;
  WARN_READY_COLOR = 2;
  LAST_PAYMENT_TYPE = 3;
  BACKUP_PATH = 4;
  AUTO_BACKUP = 5;
  MYSQL_BIN_PATH = 6;
  CLIENT_CODE_TEMPL = 7;

{ TDBOptions }

constructor TDBOptions.Create;
begin
  FPayment := TDBPaymentOptions.Create;
  FOrder := TDBOrderOptions.Create;
  FAdmin := TDBAdminOptions.Create;
  FClient := TDBClientOptions.Create;
end;

destructor TDBOptions.Destroy;
begin
  FClient.Free;
  FAdmin.Free;
  FOrder.Free;
  FPayment.Free;
  inherited;
end;

function TDBOptions.GetModified: boolean;
begin
  Result := FOrder.Modified;
end;

procedure TDBOptions.Load(AQuery: TUniQuery; AUserId: integer);
begin
  FUserId := AUserId;
  AQuery.Close;
  AQuery.SQL.Text := Format('select code, val from crm_user_options where user_id = %d order by code', [FUserId]);
  AQuery.Open;
  AQuery.First;
  FOrder.FReadyWarningDays := AQuery.Fields[1].AsInteger;
  AQuery.Next;
  FOrder.FReadyWarningColor := AQuery.Fields[1].AsInteger;
  AQuery.Next;
  FPayment.FLastPaymentType := AQuery.Fields[1].AsInteger;
  AQuery.Next;
  FAdmin.FBackupPath := AQuery.Fields[1].AsString;
  AQuery.Next;
  FAdmin.FAutoBackup := Boolean(AQuery.Fields[1].AsInteger);
  AQuery.Next;
  FAdmin.FMySQLBin := AQuery.Fields[1].AsString;
  AQuery.Next;
  FClient.FClientCodeTemplate := AQuery.Fields[1].AsString;     
end;

procedure TDBOptions.Save(AQuery: TUniQuery; AUserId: integer);
  procedure UpdateOpt(ACode: integer; AVal: string);
  begin
    AQuery.Params[0].Value := AVal;
    AQuery.Params[1].Value := ACode;
    AQuery.ExecSQL;
  end;
begin
  if AUserId <> -1 then
    FUserId := AUserId;

  AQuery.Close;
  AQuery.SQL.Text := Format('update crm_user_options set val = :val where user_id = %d and code = :code', [FUserId]);
  AQuery.Prepared := True;
  UpdateOpt(WARN_READY_DAYS, IntToStr(FOrder.FReadyWarningDays));
  UpdateOpt(WARN_READY_COLOR, IntToStr(FOrder.FReadyWarningColor));
  UpdateOpt(LAST_PAYMENT_TYPE, IntToStr(FPayment.FLastPaymentType));
  UpdateOpt(BACKUP_PATH, FAdmin.FBackupPath);
  UpdateOpt(AUTO_BACKUP, IntToStr(Integer(FAdmin.FAutoBackup)));
  UpdateOpt(MYSQL_BIN_PATH, FAdmin.FMySQLBin);
  UpdateOpt(CLIENT_CODE_TEMPL, FClient.FClientCodeTemplate);

  AQuery.Close;
  Modified := False;
end;

procedure TDBOptions.SetAdmin(const Value: TDBAdminOptions);
begin
  FAdmin := Value;
end;

procedure TDBOptions.SetClient(const Value: TDBClientOptions);
begin
  FClient := Value;
end;

procedure TDBOptions.SetModified(const Value: boolean);
begin
  FOrder.Modified := Value;
end;

procedure TDBOptions.SetOrder(const Value: TDBOrderOptions);
begin
  FOrder := Value;
end;

procedure TDBOptions.SetPayment(const Value: TDBPaymentOptions);
begin
  FPayment := Value;
end;

{ TDBOrderOptions }

constructor TDBOrderOptions.Create;
begin
  FModified := False;
end;

procedure TDBOrderOptions.SetModified(const Value: boolean);
begin
  FModified := Value;
end;

procedure TDBOrderOptions.SetReadyWarningColor(const Value: integer);
begin
  FModified := True;
  FReadyWarningColor := Value;
end;

procedure TDBOrderOptions.SetReadyWarningDays(const Value: integer);
begin
  FModified := True;
  FReadyWarningDays := Value;
end;

{ TDBPaymentOptions }

constructor TDBPaymentOptions.Create;
begin
  FLastPaymentType := 0;
  FModified := False;
end;

procedure TDBPaymentOptions.SetLastPaymentType(const Value: integer);
begin
  FLastPaymentType := Value;
end;

procedure TDBPaymentOptions.SetModified(const Value: boolean);
begin
  FModified := Value;
end;

{ TDBAdminOptions }

constructor TDBAdminOptions.Create;
begin
  FBackupPath := '';
  FAutoBackup := False;
  FModified := False;
end;

procedure TDBAdminOptions.SetAutoBackup(const Value: boolean);
begin
  FAutoBackup := Value;
  FModified := True;
end;

procedure TDBAdminOptions.SetBackupPath(const Value: string);
begin
  FBackupPath := Value;
  FModified := True;
end;

procedure TDBAdminOptions.SetModified(const Value: boolean);
begin
  FModified := Value;
end;

procedure TDBAdminOptions.SetMySQLBin(const Value: string);
begin
  FMySQLBin := Value;
  FModified := True;
end;

{ TDBClientOptions }

constructor TDBClientOptions.Create;
begin
  FClientCodeTemplate := '';
  FModified := False;
end;

procedure TDBClientOptions.SetClientCodeTemplate(const Value: string);
begin
  FClientCodeTemplate := Value;
  FModified := True;
end;

procedure TDBClientOptions.SetModified(const Value: boolean);
begin
  FModified := Value;
end;

end.

